﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Data;
using System.Data.OleDb;

namespace WindowsFormsApplication1
{
    public class XMLStuff
    {
       public static void CheckXMLIntegrity()
       {
           BinaryReader binReader = new BinaryReader(File.Open(Form1.xmlLocation, FileMode.Open), System.Text.Encoding.BigEndianUnicode);
           binReader.BaseStream.Seek(binReader.BaseStream.Length - 1, SeekOrigin.Begin);

           int readInt;
           readInt = binReader.ReadByte();

           if (readInt.ToString("X") != "20")
           {
               binReader.Close();
               string temp;

               StreamReader fileStream = new StreamReader(Form1.xmlLocation);
               temp = fileStream.ReadToEnd();
               fileStream.Close();

               File.WriteAllText(Form1.xmlLocation, temp);

           }

           else
           {
               binReader.Close();
           }

       }


       public static void InitialXMLRead(bool fsd2)
       {
           StreamReader oRead = File.OpenText(Form1.xmlLocation);

           string xmlStream = oRead.ReadToEnd();
           oRead.Close();

           if (xmlStream.Contains("&"))
           {
               xmlStream = xmlStream.Replace("&", "and");
           }

           File.WriteAllText("temp_content.xml", xmlStream);
           XmlDocument xDoc = new XmlDocument();
           xDoc.Load("temp_content.xml");

           if (xDoc.DocumentElement.Name == "freestyle")
           {
               
               if (fsd2 == false)
               {
                   XmlNodeList TitleID = xDoc.GetElementsByTagName("id");
                   XmlNodeList GameName = xDoc.GetElementsByTagName("title");
                   XmlNodeList MediaID = xDoc.GetElementsByTagName("mediaid");
                   int Count = TitleID.Count - 1;

                   for (int index = 0; index <= Count; index++)
                   {
                       int MediaIDInt;
                       MediaIDInt = Convert.ToInt32(MediaID.Item(index).InnerText);

                       Form1.GamesList.Items.Add(GameName.Item(index).InnerText);
                       Form1.GamesList.Items[index].SubItems.Add(TitleID.Item(index).InnerText.ToUpper());
                       Form1.GamesList.Items[index].SubItems.Add(MediaIDInt.ToString("X"));
                   }
               }
               else
               {
                   XmlNodeList TitleID = xDoc.GetElementsByTagName("id");
                   XmlNodeList GameType = xDoc.GetElementsByTagName("type");
                   XmlNodeList Game = xDoc.GetElementsByTagName("game");
                   int Count = Game.Count - 1;

                   int ItemCount = 0;

                   for (int index = 0; index <= Count; index++)
                   {
                       int GameTypeID = Convert.ToInt16(Game.Item(index).Attributes.Item(0).InnerText);
                       if((GameTypeID == 0)||(GameTypeID == 1)||(GameTypeID == 2))
                       {
                           //if (Form1.getnames == true)
                           //{
                           //    Form1.GamesList.Items.Add(GetGameName(Game.Item(index).Attributes.Item(1).InnerText.ToUpper()));
                           //}
                           //else
                           //{
                               Form1.GamesList.Items.Add("");
                           //}
                         Form1.GamesList.Items[ItemCount].SubItems.Add(Game.Item(index).Attributes.Item(1).InnerText.ToUpper());
                         Form1.GamesList.Items[ItemCount].SubItems.Add(GetGameType(Convert.ToInt16(Game.Item(index).Attributes.Item(0).InnerText)));
                         ItemCount++;
                       }
                   }
                   
                   //Form1.GameNamebackgroundWorker.RunWorkerAsync(sender, e);
               }
           }
               
           else
           {

           }


       }

       public void AddTUtoLog()
       {
       //XmlDocument xDoc;

        XmlTextWriter textWriter = new XmlTextWriter("tulist.xml", null);
        textWriter.WriteStartDocument();
        textWriter.WriteComment("Title Downloader update log");
        textWriter.WriteStartElement("Title_Downloader");
        textWriter.WriteStartElement("Game");
        textWriter.WriteStartElement("Game_Name");
        //textWriter.WriteString(InputBox("value"));
        textWriter.WriteEndElement();
        textWriter.WriteStartElement("Title_ID");
        textWriter.WriteEndElement();

        textWriter.Close();
       }

       public static string GetGameType(int GameTypeNumber)
       {
           if(GameTypeNumber == 0)
           {
               return "XBLA";
           }
           else if(GameTypeNumber == 1)
           {
               return "XBOX 360";
           }
           else if(GameTypeNumber == 2)
           {
               return "XBOX 1";
           }
           else if (GameTypeNumber == 3)
           {
               return "HOMEBREW";
           }
           else if (GameTypeNumber == 4)
           {
               return "EMULATOR";
           }
           else
           {
               return "UNKNOWN";
           }
       }

       public static string GetGameName(string TitleID)
       {
           try
           {
               XmlDocument xDoc = new XmlDocument();
               xDoc.Load("GameDataBase.xml");

               XmlNodeList Game = xDoc.GetElementsByTagName("Game");
               //XmlNodeList TitleIDxml = xDoc.GetElementsByTagName("TitleID");
               //XmlNodeList GameNamexml = xDoc.GetElementsByTagName("GameName");
               int Count = Game.Count - 1;

               for (int index = 0; index <= Count; index++)
               {
                   string blah = Game.Item(index).ChildNodes.Item(1).InnerText.ToUpper();
                   if (TitleID.CompareTo(blah) == 0)  //(TitleID == blah) ;//Game.Item(index).ChildNodes.Item(0).InnerText.ToUpper()) ; //TitleIDxml.Item(index).InnerText.ToUpper());
                   {
                       return Game.Item(index).ChildNodes.Item(0).InnerText; //GameNamexml.Item(index).ChildNodes.Item(index).InnerText;
                   }
               }
               return "Not found";
           }
           catch(Exception ex)
           {
               return ex.Message;
           }
       }
    }
}
